package cloudcar.yunchexing.com.cloudlibrary;

import android.content.Context;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

/**
 * Created by lixiaoming on 16/3/30.
 */
public class CXImageLoader {

    private DisplayImageOptions defaultOptions;

    private CXImageLoader() {
    }

    private static CXImageLoader mInstance;

    public static CXImageLoader getInstance() {
        if (mInstance == null) {
            synchronized (CXImageLoader.class) {
                mInstance = new CXImageLoader();
            }
        }
        return mInstance;
    }

    /*
    * context: 一定要使用ApplicationContext
    * */
    public void init(Context context) {
        /**保存路径设置***/
        File file = StorageUtils.getCacheDirectory(context);

        /**常规设置***/
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
                .threadPoolSize(3) // default
                .threadPriority(Thread.NORM_PRIORITY - 1) // default
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13) // default
                .diskCache(new UnlimitedDiskCache(file))
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
                .imageDownloader(new BaseImageDownloader(context)) // default
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                .writeDebugLogs()
                .build();
        /***全局初始化此配***/
        ImageLoader.getInstance().init(config);

        /**加载图片时是否使用缓存**/
        defaultOptions = new DisplayImageOptions.Builder()
//                .showImageOnFail(R.mipmap.ic_launcher)//加载图片失败时的图片
                .cacheInMemory(true)                               //启用内存缓存
                .cacheOnDisk(true)                                 //启用外存缓存
                .build();

    }

    /*
     *显示网络图片,默认使用内存缓存和sd卡缓存
     * */
    public void displayImage(String url, ImageView view) {
        ImageLoader.getInstance().displayImage(url, view, defaultOptions);
    }

    /*
    *
    * */
    public void displayImage(String url, ImageView view, DisplayImageOptions options) {
        ImageLoader.getInstance().displayImage(url, view, options);
    }

}
